<template>
    <van-nav-bar title="邮箱认证" left-text="" left-arrow @click-left="goBack" />
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field v-model="LoginUser.email" name="email" label="邮箱" placeholder="请输入邮箱帐号" readonly />
            <van-field v-model="code" center clearable label="邮箱验证码" placeholder="请输入邮箱验证码">
                <template #button>
                    <van-button size="small" type="primary" @click="send" :disabled="isShow">
                        <span v-if="!isShow">发送验证码</span>
                        <van-count-down v-show="isShow" ref="countDown" millisecond :time="time" :auto-start="false" format="ss秒"
                            @finish="onFinish" />
                    </van-button>
                </template>
            </van-field>
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block color="linear-gradient(to right, #ff6034, #ee0a24)" native-type="submit">
                提交
            </van-button>
        </div>
    </van-form>

</template>
<script setup>
import { showSuccessToast } from 'vant';
import { getCurrentInstance, reactive, ref } from 'vue';

const { proxy } =getCurrentInstance();
const LoginUser=reactive(proxy.$cookies.get('LoginUser'));
const goBack=()=>{
    proxy.$router.push('/business/index')
}
const code =ref('');
const isShow=ref(false);
const countDown=ref(null);
const time=ref(10000)
const onFinish = () =>{
    countDown.value.reset();
    isShow.value = false
}
const send =async () =>{
    isShow.value = true
    countDown.value.start();
    let result =await Api.sendemail({
     busid:LoginUser.id,
    })
    console.log(LoginUser)
    if(result.code==1){
        showSuccessToast(result.msg)
    }else{
        showSuccessToast(result.msg)
    }
}
const onSubmit =async (values) => {
    let result=await Api.email({
        busid:LoginUser.id,
        code:code.value
    })
    if(result.code==1){
        showSuccessToast({
            message:result.msg,
            onClose:()=>{
                proxy.$router.push('/business/index')
            }
        })
    }else{
        showSuccessToast(result.msg)
    }
    console.log(code);
};
</script>